package com.kunchi.platform.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kunchi.platform.model.data.CurrentData;
import com.kunchi.platform.model.data.HistoryData;
import com.kunchi.platform.service.DataService;
import com.kunchi.platform.service.LoginService;
import com.kunchi.platform.utils.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by denniszhou on 2017/6/24.
 */
@Service("dataService")
public class DataServiceImpl implements DataService {
    @Resource
    LoginService loginService;

    @Autowired
    Environment env;

    @Override
    public List<CurrentData> getCurrentData(String deviceId) throws IOException {
        List<CurrentData> historyDataList = new ArrayList<>();
        String url = env.getRequiredProperty("BASE_URL") +
                "/currentdata?token={0}&hash={1}&deviceid={2}";
        String token = loginService.login();
        String response = HttpUtils.getHttpGetResponse(MessageFormat.format(
                url, token, env.getRequiredProperty("LOGIN_HASH"), deviceId));
        JSONArray historyData = JSONObject.parseObject(response).getJSONArray("data");
        for (int i = 0; i < historyData.size(); i++) {
            historyDataList.add(JSONObject.parseObject(historyData.getString(i), CurrentData.class));
        }
        return historyDataList;
    }

    @Override
    public HashMap<String, List<HistoryData>> getHistoryData(
            String deviceId, Long startTimestamp, Long endTimestamp) throws IOException {
        HashMap<String, List<HistoryData>> historyDataHashMap = new HashMap<>();
        String url = env.getRequiredProperty("BASE_URL") +
                "/history/forids?token={0}&hash={1}&deviceid={2}";
        String token = loginService.login();
        String response = HttpUtils.getHttpGetResponse(MessageFormat.format(
                url, token, env.getRequiredProperty("LOGIN_HASH"), deviceId));
        JSONObject content = JSONObject.parseObject(response).getJSONObject("data");
        for (String itemId : content.keySet()) {
            JSONArray historyDataList = content.getJSONArray(itemId);
            for (int i = 0; i < historyDataList.size(); i++) {
                if (!historyDataHashMap.containsKey(itemId)) {
                    historyDataHashMap.put(itemId, new ArrayList<>());
                } else {
                    HistoryData historyData = JSON.parseObject(historyDataList.getString(i), HistoryData.class);
                    if ((startTimestamp == null || historyData.getHtime().after(new Date(startTimestamp))) &&
                            (endTimestamp == null || historyData.getHtime().before(new Date(endTimestamp)))) {
                        historyDataHashMap.get(itemId).add(historyData);
                    }
                }
            }
        }
        return historyDataHashMap;
    }


    @Override
    public List<HistoryData> getHistoryDataByDataItemId(
            String deviceId, String dataItemId, Long startTimestamp, Long endTimestamp) throws IOException {
        List<HistoryData> historyDataList = new ArrayList<>();
        String url = env.getRequiredProperty("BASE_URL") +
                "/historydata?token={0}&hash={1}&deviceid={2}&dataitemid={3}";
        String token = loginService.login();
        String response = HttpUtils.getHttpGetResponse(MessageFormat.format(
                url, token, env.getRequiredProperty("LOGIN_HASH"), deviceId, dataItemId));
        JSONArray historyDatas = JSONObject.parseObject(response).getJSONArray("data");
        for (int i = 0; i < historyDatas.size(); i++) {
            HistoryData historyData = JSONObject.parseObject(historyDatas.getString(i), HistoryData.class);
            if ((startTimestamp == null || historyData.getHtime().after(new Date(startTimestamp))) &&
                    (endTimestamp == null || historyData.getHtime().before(new Date(endTimestamp)))) {
                historyDataList.add(historyData);
            }
        }
        return historyDataList;
    }
}
